<?php

$rootdir = '../';
include_once('session.php');

global $desql;
$desql->Connect();

function CreateCodePromo($Code, $Type, $Param, $Nombre)
{
	global $desql;
	
	$result = $desql->Query('INSERT INTO codespromo(code,
												type,
												param,
												nombre) 
										VALUES(\''. mysql_real_escape_string($Code) . '\',
												\''. mysql_real_escape_string($Type) . '\',
												\''. mysql_real_escape_string($Param) . '\',
												\'' . mysql_real_escape_string($Nombre) . '\'
										)');
	if($result)
		return true;
	else
		return false;
}

function UpdateCodePromo($code)
{
	global $desql;
	
	return $desql->Query('UPDATE codespromo SET code=\'' . mysql_real_escape_string($code['code']) . '\',
										type=\'' . mysql_real_escape_string($code['type']) . '\',
										param=' . mysql_real_escape_string($code['param']) . ', 
										nombre=\'' . mysql_real_escape_string($code['nombre']) . '\'
									WHERE id=' . mysql_real_escape_string($code['id']));
}

function GetCodePromoByCode($Code)
{
	global $desql;
	
	$result = $desql->Query('SELECT * FROM codespromo WHERE code=\'' . mysql_real_escape_string($Code) . '\'');
	
	if($result == false)
		return false;

	if($t = mysql_fetch_array($result))
		return $t;
	else
		return false;
}

function GetAllCodesPromo()
{
	global $desql;
	$codespromo = array();
	
	$answer = $desql->Query('SELECT * FROM codespromo');
	if($answer == false)
		return false;
		
	while($codepromo = mysql_fetch_array($answer))
		$codespromo[$codepromo['id']] = $codepromo;
	
	return $codespromo;
}

function DeleteCodepromoById($Id)
{
	global $desql;
	
	if(is_numeric($Id) == false)
		return false;
		
	return $desql->Query('DELETE FROM codespromo WHERE id=' . $Id);
}

function GetAdminCodespromoHTMLContent()
{
	$HTML = '<div style="margin-top: 20px; margin-left: 20px;">';
		
	$HTML .= '
		<table style="font-size: 80%; width: 90%; text-align: center;">
			<tr>
				<th style="border: 1px solid gray;">Code</th>
				<th style="border: 1px solid gray;">Type</th>
				<th style="border: 1px solid gray;">Paramètre</th>
				<th style="border: 1px solid gray;">Nombre</th>
				<th style="border: 1px solid gray;">Options</th>
			</tr>
			<tr>
				<td  style="border: 1px solid gray;"></td>
				<td  style="border: 1px solid gray;"></td>
				<td  style="border: 1px solid gray;"></td>
				<td  style="border: 1px solid gray;"></td>
				<td  style="border: 1px solid gray;"><div id="icone-nouveau"><a href="cadre_admin.php?type=4&new=1"></a></div></td>
			<tr>' . GetAdminCodepromoHTMLContent() . '
		</table>
	';
	return $HTML . '</div>';
}

function GetAdminCodepromoHTMLContent()
{
	$codes = GetAllCodesPromo();
	$HTML = '';
	
	if($codes == false)
		return '';
	
	foreach($codes as $code)
	{
		$HTML .= '
			<tr>
				<td style="border: 1px solid gray;">' . $code['code'] . '</td>
				<td style="border: 1px solid gray;">' . $code['type'] . '</td>
				<td style="border: 1px solid gray;">' . $code['param'] . '</td>
				<td style="border: 1px solid gray;">' . $code['nombre'] . '</td>
				<td style="border: 1px solid gray;">
					<div id="icone-supprimer">
						<a href="cadre_admin.php?type=4&deleteid=' . $code['id'] . '" onClick="return(confirm(\'Etes-vous sûr de vouloir supprimer ce code promo?\'));"></a>
					</div>
				</td>
			</tr>
		';
	}

	return $HTML;
}

function GetCreateCodeForm()
{
	$HTML = '
		<h4>Nouveau code promo</h4>
		<form style="margin-left: 20px; margin-top: 20px; font-size: 80%;" method="post">
			<table style="margin: auto;">
				<tr>
					<td style="width: 80px;"><label for="code">Code :</label></td>
					<td><input type="text" tabindex="1" name="code" size="20" /><br /></td>
				</tr>
				<tr>
					<td><label for="type">Type :</label></td>
					<td>
						<select tabindex="2" name="type">
							<option value="Reduction fixe">Réduction fixe</option>
							<option value="Pourcentage">Pourcentage</option>
							<option value="Port offert">Frais de port offerts</option>
						</select>
					</td>	
				</tr>
				<tr>
					<td><label for="parametre">Parametre :</label></td>
					<td><input type="text" tabindex="3" name="parametre" size="6"/><br /></td>
					<td>Si type = réduction fixe : entrer la remise, en euros, sans le symbole €<br />
					    Si type = pourcentage : entrer le pourcentage, sans le symbole %, par exemple, 30<br />
						Si type = port offert : laisser ce champ vide<br /><br /></td>
				</tr>
				<tr>
					<td><label for="nombre">Nombre :</label></td>
					<td><input type="text" tabindex="4" name="nombre" size="10" /><br /></td>
					<td>-1 : Le code fonctionne tant qu\'il existe, utilisation illimitée. Le supprimer pour le désactiver<br />
					    Entier positif : nombre de fois que le code fonctionne avant de se désactiver.</td>
				</tr>
				<tr>
					<td></td>
					<td><br /><input type="submit" name="send" tabindex="5" value="Envoyer" /></td>
				</tr>
			</table>
		</form>
	';
	
	return $HTML;
}

?>